package com.zr.controller;
/**
 * 
 * @author CY
 *
 */
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zr.model.User;
import com.zr.service.UserService;
import com.zr.serviceImpl.UserServiceImpl;



public class LoginController extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	UserService user = new UserServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("utf8");
		HttpSession  session = req.getSession();
		String userNum = req.getParameter("userName");
		String userPsw = req.getParameter("password");
		session.setAttribute("eid", userNum);
		session.setAttribute("upsw", userPsw);
		String rm = req.getParameter("rm");
		PrintWriter pw = resp.getWriter();
		User User = user.findUserByNumAndPassword(userNum, userPsw);
		if(User.getUsernum()>0){
			 if("on".equals(rm)){
				 Cookie unames=new Cookie("uname",userNum);
				 unames.setMaxAge(60*60);
				 Cookie cookie=new Cookie("upsw",userPsw);
				 cookie.setMaxAge(60*60);
				 //把值加到磁盘当中
				 resp.addCookie(unames);
				 resp.addCookie(cookie);
			 }
			session.setAttribute("user", User);
			pw.write("1");
		}else{
			pw.write("0");
		}
	}
}
